package cn.dkk.structure_design.combination.pojo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dkk
 * @date 2022/7/18
 * @desc
 */
public class Menu extends MenuComponent{

    private List<MenuComponent> children=new ArrayList<>();

    public Menu(String name,int level){
        this.setName(name);
        this.setLevel(level);
    }

    @Override
    public void addChild(MenuComponent menuComponent) {
        children.add(menuComponent);
    }

    @Override
    public MenuComponent getChild(int index) {
        return children.get(index);
    }

    @Override
    public void remove(int index) {
        children.remove(index);
    }

    /**
     * 打印该菜单及子菜单项名称,巧妙之处就在于
     * 充分的利用多态,无论子菜单项是菜单还是非菜单,都直接调用print方法
     * 即调用某一个节点的print,即可打印其下所有节点
     */
    @Override
    public void print() {
        System.out.println(getName());
        for (MenuComponent child : children) {
            // 递归打印
            child.print();
        }
    }
}
